package com.gxa.modules.service.impl;

import com.gxa.common.utils.Constants;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.OrderMapper;
import com.gxa.modules.pojo.Order;
import com.gxa.modules.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Auth WK
 * @DATE 2022/9/13 15:29
 * @DESCRIBE
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    R r = new R();
    JsonResult jsonResult = new JsonResult();

    /**
     * 1.订单列表查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findOrderList(Integer currentPage, Integer pageSize) {
        //查询总条数
        int count = orderMapper.findCountOfOrder(Constants.STATUS_DELECT);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Map> orderList = orderMapper.findOrderList(start,off, Constants.STATUS_DELECT);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(orderList);
        return jsonResult;

    }

    /**
     * 2.订单审核
     * @param orderId
     * @return
     */
    @Override
    public R checkOrder(Integer orderId) {
        int result = orderMapper.checkOrder(orderId, Constants.STATUS_audit);
        if(result>0){
            r.setCode(200);
            r.setMsg("审核成功");
        }
        return r;
    }

    /**
     * 3.订单修改
     * @param order
     * @return
     */
    @Override
    public R updateOrder(Order order) {
        int result = orderMapper.updateOrder(order);
        if(result>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    /**
     * 4.订单作废
     * @param orderId
     * @return
     */
    @Override
    public R delectOrder(Integer orderId) {
        int result = orderMapper.delectOrder(orderId, Constants.STATUS_DELECT);
        if(result>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }

    /**
     * 5.查看订单详情
     * @param orderId
     * @return
     */
    @Override
    public JsonResult findOrder(Integer orderId) {
        Order order = orderMapper.findOrder(orderId);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(order);
        return jsonResult;
    }

    /**
     * 6.条件查询订单
     * @param customerName
     * @param status
     * @param payTime
     * @return
     */
    @Override
    public JsonResult findOrderByCondition(String customerName, Integer status, String payTime, Integer currentPage, Integer pageSize) {
        //查询总条数
        int count = orderMapper.findCountOfOrderByCondition(customerName,status,payTime, Constants.STATUS_DELECT);
        Integer start = (currentPage - 1) * pageSize;//分页第一个参数
        Integer off = pageSize; //分页第二个参数
        List<Order> orderList = orderMapper.findOrderByCondition(customerName,status,payTime,start,off, Constants.STATUS_DELECT);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(orderList);
        return jsonResult;
    }
}
